package com.xjl.meituwidget;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @author x
 * @Description
 */
public class SpHelper {
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;

    public void init (Context context, String name, int mode) {
        mPreferences = context.getSharedPreferences(name, mode);
    }

    private SpHelper () {
    }

    public static SpHelper getInstance () {
        return Holder.HELPER;
    }

    public SpHelper edit () {
        mEditor = mPreferences.edit();
        return this;
    }

    public void commit () {
        mEditor.apply();
        mEditor = null;
    }

    public SharedPreferences getPreferences () {
        return mPreferences;
    }

    public SpHelper putString (String key, String value) {
        mEditor.putString(key, value);
        return this;
    }

    public SpHelper putLong (String key, long value) {
        mEditor.putLong(key, value);
        return this;
    }

    public SpHelper putInt (String key, int value) {
        mEditor.putInt(key, value);
        return this;
    }

    public SpHelper putBoolean (String key, boolean value) {
        mEditor.putBoolean(key, value);
        return this;
    }

    public SpHelper putStringSet (String key, Set<String> value) {
        mEditor.putStringSet(key, value);
        return this;
    }

    public <T extends Serializable> SpHelper putObject (String key, T obj) {
        // obj必须实现Serializable接口，否则会出问题
        if (obj != null) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(obj);
                String string64 = new String(Base64.encode(baos.toByteArray(),
                        0));
                return putString(key, string64);
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("SpHelper", "putObject obj is null!!!!!!!!!!!!!");
            return putString(key, "");
        }
        return this;
    }

    public String getString (String key) {
        return mPreferences.getString(key, "");
    }

    public long getLong (String key) {
        return mPreferences.getLong(key, 0L);
    }

    public int getInt (String key) {
        return mPreferences.getInt(key, 0);
    }

    public boolean getBoolean (String key) {
        return mPreferences.getBoolean(key, false);
    }

    public Set<String> getStringSet (String key) {
        return mPreferences.getStringSet(key, new HashSet<String>());
    }

    public <T extends Serializable> T getObject (String key) {
        Object obj = null;
        try {
            String base64 = getString(key);
            if (base64.equals("")) {
                return null;
            }
            byte[] base64Bytes = Base64.decode(base64.getBytes(), 1);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            obj = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T) obj;
    }


    public SpHelper clear () {
        mEditor.clear();
        return this;
    }

    private static class Holder {
        private static final SpHelper HELPER = new SpHelper();
    }
}
